﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GameService;
using frag_boardgame;

namespace Website
{
    public partial class frag : System.Web.UI.MasterPage
    {
        public Facebook.Rest.Api Api;
        private Facebook.Session.ConnectSession connectSession;
        private Facebook.Schema.user fbUser;
        private bool initialized = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            Initialize();
        }

        public bool IsConnected()
        {
            return ConnectSession != null && ConnectSession.IsConnected();
        }

        public void Initialize()
        {
            if (!ConnectSession.IsConnected())
            {
                lblStatus.Text = "Please sign-in with Facebook.";
                loginPanel.Visible = true;
            }
            else
            {
                lblStatus.Text = "Welcome, " + FbUser.name;
                loginPanel.Visible = false;
            }

        }

        public Facebook.Schema.user FbUser
        {
            get
            {
                fbUser = (Facebook.Schema.user)Session["FbUser"];
                if (fbUser == null)
                {
                    if (ConnectSession.IsConnected())
                    {
                        fbUser = FbApi.Users.GetInfo();
                        Session["FbUser"] = fbUser;
                        GameManager.currentUserFacebookUid = fbUser.uid;
                    }
                }
                return fbUser;
            }
        }

        public Facebook.Rest.Api FbApi
        {
            get
            {
                Api = (Facebook.Rest.Api)Session["FbApi"];
                if (Api == null)
                {
                    Api = new Facebook.Rest.Api(ConnectSession);
                    Session["FbApi"] = Api;
                }
                return Api;
            }
        }

        public Facebook.Session.ConnectSession ConnectSession
        {
            get
            {
                connectSession = (Facebook.Session.ConnectSession)Session["ConnectSession"];
                if (connectSession == null)
                {
                    connectSession = new Facebook.Session.ConnectSession(ConfigurationManager.AppSettings["ApiKey"], ConfigurationManager.AppSettings["Secret"]);
                    Session["ConnectSession"] = connectSession;
                }
                return connectSession;
            }
        }

         


        private GameManager gameManager;
        public GameManager GameManager
        {
            get
            {
                if (gameManager == null)
                {
                    gameManager = Application["GameManager"] as GameManager;
                    if (gameManager == null)
                    {
                        gameManager = new GameManager();
                        Application["GameManager"] = gameManager;
                    }
                }
                return gameManager;
            }
        }


        public bool IsLoggedIn
        {
            get { return Session["UserInfo"] != null; }
        }

        public void SetUser(string username)
        {
            Session["UserInfo"] = GameManager.GetUserInfo(username);
        }

        public bool IsPlrConfCreated
        {
            get { return (string)Session["Created"] == "yes"; }
            set
            {
                if (value)
                    Session["Created"] = "yes";
                else
                    Session["Created"] = "no";
            }

        }
        public JoinRoomResult JoinRoom(string roomName)
        {
            JoinRoomResult res = gameManager.JoinRoom(roomName, GetUserInfo().config.Name);
            if (res == JoinRoomResult.Succeed)
                Session["Room"] = gameManager.rooms[roomName];
            return res;
        }

        public Room GetCurrentRoom()
        {
            return Session["Room"] as Room;
        }

        public UserInfo GetUserInfo()
        {
            return Session["UserInfo"] as UserInfo;
        }

        public bool IsGuest
        {
            get
            {
                return Session["IsGuest"] != null;
            }
            set
            {
                Session["IsGuest"] = value ? new Object() : null;
            }
        }

        public bool IsFacebook
        {
            get
            {
                return Session["IsFacebook"] != null;
            }
            set
            {
                Session["IsFacebook"] = value ? new Object() : null;
            }
        }

        public void LeaveRoom()
        {
            GameManager.LeaveRoom(GetCurrentRoom(), GetUserInfo());
            Session["Room"] = null;
        }
        public void SetPlayerConfiguration(PlayerConfiguration conf)
        {
            GetCurrentRoom().Info.config.addPlayer(conf);
            GetUserInfo().config = conf;

        }

        public void Logout()
        {
            if (IsLoggedIn)
            {
                GameManager.RemoveUser(GetUserInfo().config.Name);
            }
            Session.Abandon();
        }

        public int GetPlayerScore()
        {
            string name = GetUserInfo().config.Name;
            if (name.ToLower().StartsWith("guest"))
                return 0;

            return GameManager.GetPlayerScore(name);
        }

        public void WinGame()
        {
            string name = GetUserInfo().config.Name;
            if (!name.ToLower().StartsWith("guest"))
                GameManager.CalculateNewScore(name, GetCurrentRoom());
        }



    }
}
